package cn.school.demo.ui.article.entity

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.MyApplication
import cn.school.demo.pojo.Article
import cn.school.demo.pojo.Comment
import cn.school.demo.pojo.Result
import cn.school.demo.repository.*

class ArticleEntityViewModel : ViewModel() {

    private val _isFabulous = MutableLiveData(false)
    val isFabulous: LiveData<Boolean> get() = _isFabulous

    private val _isCollect = MutableLiveData(false)
    val isCollect: LiveData<Boolean> get() = _isCollect

    private val _commentText = MutableLiveData<String>()
    val commentText: LiveData<String> get() = _commentText

    private val _commentList = mutableListOf<Comment>()
    val commentList: MutableList<Comment> get() = _commentList

    private var _page = 1

    private var _size = 15
    val size: Int get() = _size


    fun isCF(): LiveData<Map<String, Boolean>> {
        MyApplication.article?.let {
            return DataRepository().isCF(MyApplication.user!!.sfNumber, it.articleId)
        } ?: let {
            return MutableLiveData(mutableMapOf("fabulous" to false, "collect" to false))
        }
    }

    /*============点赞相关方法===================*/
    fun updateFabulous(boolean: Boolean) {
        _isFabulous.value = boolean
    }

    fun saveMyLove(): LiveData<Result> {
        MyApplication.article?.let {
            return FabulousRepository().saveFabulousById(
                MyApplication.user!!.sfNumber,
                MyApplication.article!!.articleId
            )
        } ?: let {
            return MutableLiveData<Result>(Result(false, 404, "articleId为空！！", mutableMapOf()))
        }
    }

    fun cancelMyLove(): LiveData<Result> {
        MyApplication.article?.let {
            return FabulousRepository().cancelFabulousById(
                MyApplication.user!!.sfNumber,
                MyApplication.article!!.articleId
            )
        } ?: let {
            return MutableLiveData<Result>(Result(false, 404, "articleId为空！！", mutableMapOf()))
        }
    }

    /*============收藏相关方法===================*/
    fun updateCollect(boolean: Boolean) {
        _isCollect.value = boolean
    }

    fun saveCollectById(): LiveData<Result> {
        MyApplication.article?.let {
            return CollectRepository().saveCollectById(
                MyApplication.user!!.sfNumber,
                it.articleId,
                it.articleName
            )
        } ?: let {
            return MutableLiveData<Result>(Result(false, 404, "articleId为空！！", mutableMapOf()))
        }
    }

    fun cancelCollectById(): LiveData<Result> {
        MyApplication.article?.let {
            return CollectRepository().cancelCollectById(
                MyApplication.user!!.sfNumber,
                it.articleId
            )
        } ?: let {
            return MutableLiveData<Result>(Result(false, 404, "articleId为空！！", mutableMapOf()))
        }
    }

    /*============浏览历史相关方法===================*/
    fun saveBrowseInfo(): LiveData<Boolean> {
        //保存浏览历史，若成功保存积分
        if (MyApplication.article != null) {
            return HistoryRepository().saveBrowseInfo(
                MyApplication.user!!.sfNumber,
                MyApplication.article!!.articleId
            )
        } else return MutableLiveData(false)
    }

    /*============评论相关方法===================*/
    fun setCommentText(t: String) {
        _commentText.value = t
    }

    fun updateCommentList(list: MutableList<Comment>) {
        _commentList.clear()
        _commentList.addAll(list)
    }

    fun saveCommentById(): LiveData<Result> {
        MyApplication.apply {
            return if (article != null) {
                CommentRepository().saveUserCommentById(
                    user!!.sfNumber,
                    _commentText.value!!,
                    article!!.articleId
                )
            } else MutableLiveData<Result>(
                Result(
                    false,
                    404,
                    "articleId为空！！",
                    mutableMapOf()
                )
            )
        }
    }

    fun getCommentListById(): LiveData<MutableList<Comment>> {
        return if (MyApplication.article != null) {
            CommentRepository().getUserCommentById(
                _page,
                _size * _page,
                MyApplication.article!!.articleId
            )
        } else
            MutableLiveData(mutableListOf())
    }

    fun getNextCommentListById(): LiveData<MutableList<Comment>> {
        return if (MyApplication.article != null) {
            CommentRepository().getUserCommentById(
                _page,
                _size ,
                MyApplication.article!!.articleId
            )
        } else
            MutableLiveData(mutableListOf())
    }

    fun nextPage() = ++_page

    fun addCommentList(list: MutableList<Comment>) {
        _commentList.addAll(list)
    }

    fun getArticleById(): LiveData<Article> {
        return ArticleRepository().getArticleById(MyApplication.articleId)
    }

    fun saveArticleHitById() =
        ArticleRepository().saveArticleHitById(MyApplication.article!!.articleId)

}